package com.jiang.myfm.adapters;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by dell on 2016/12/7 0007.
 */
public class MainHeadTwoAdapter extends FragmentPagerAdapter {
    private List<Fragment> mFragments;
//    private FragmentManager fm;
    public MainHeadTwoAdapter(FragmentManager fm) {
        super(fm);
//        this.fm=fm;
    }
    //    public MainHeadTwoAdapter(FragmentManager fm, List<Fragment> fragments) {
//        super(fm);
//        mFragments = fragments;
//    }

    @Override
    public Fragment getItem(int position) {
        return mFragments!=null?mFragments.get(position):null;
    }

    @Override
    public int getCount() {
        return mFragments!=null?mFragments.size():0;
    }
    public void addAll(List<Fragment> fragments){
        mFragments = fragments;
        notifyDataSetChanged();
//        Log.d("flag","第二个");
    }
    public void reSetData(){
        mFragments=null;
        notifyDataSetChanged();
    }
//
//    @Override
//    public Object instantiateItem(ViewGroup container, int position) {
//        if (position == 0)
//            removeFragment(container,position);
//        return super.instantiateItem(container, position);
//    }
//    private void removeFragment(ViewGroup container,int index) {
//        String tag = getFragmentTag(container.getId(), index);
//        Fragment fragment = fm.findFragmentByTag(tag);
//        if (fragment == null)
//            return;
//        FragmentTransaction ft = fm.beginTransaction();
//        ft.remove(fragment);
//        ft.commit();
//        ft = null;
//        fm.executePendingTransactions();
//    }
//    private String getFragmentTag(int viewId, int index) {
//        try {
//            Class<FragmentPagerAdapter> cls = FragmentPagerAdapter.class;
//            Class<?>[] parameterTypes = { int.class, long.class };
//            Method method = cls.getDeclaredMethod("makeFragmentName",
//                    parameterTypes);
//            method.setAccessible(true);
//            String tag = (String) method.invoke(this, viewId, index);
//            return tag;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }
//
//    @Override
//    public int getItemPosition(Object object) {
//        return POSITION_NONE;
//    }
}
